package com.greenline.push.home.controllers.message;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenline.push.common.json.GridJsonData;
import com.greenline.push.core.pushcenter.MsgHisManager;
import com.greenline.push.dal.pushcenter.dataobject.MsgHisDO;
import com.greenline.push.dal.query.MsgHisQuery;

@Controller
@RequestMapping(value = "/order/history")
public class MsgHisController {

    @Resource
    private MsgHisManager MsgHispManager;
    private final Log logger = LogFactory.getLog(MsgHisController.class);

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String queryRecord(ModelMap model) {
        return "msghis/index";
    }

    @RequestMapping("/query.json")
    @ResponseBody
    public GridJsonData<MsgHisDO> listRecord(MsgHisQuery query,
            @RequestParam(value = "page", defaultValue = "1", required = false) String pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "20", required = false) String pageSize, String start, String end) {
        Integer status = query.getStatus();
        if (status == -1) {
            query.setStatus(null);
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        // set start time
        if (StringUtils.isNotBlank(start)) {
            try {
                query.setStartTime(format.parse(start));
            } catch (ParseException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            }
        }
        // set end time
        if (StringUtils.isNotBlank(end)) {
            try {
                query.setEndTime(format.parse(end));
            } catch (ParseException e) {
                e.printStackTrace();
                logger.error(e.getMessage());
            }
        }
        query.setPageIndex(Integer.parseInt(pageIndex));
        query.setPageSize(Integer.parseInt(pageSize));

        GridJsonData<MsgHisDO> ret = new GridJsonData<MsgHisDO>();
        MsgHispManager.listByQuery(query);
        //
        ret.setQuery(query);
        return ret;
    }

    // @RequestMapping(value = "/query/json", method = RequestMethod.GET)
    // @ResponseBody
    // public GridJsonData<PackageDefineDO> queryRecordlJson(
    // @RequestParam(value = "page", defaultValue = "1", required = false)
    // String pageIndex,
    // @RequestParam(value = "pageSize", defaultValue = "10", required = false)
    // String pageSize,
    // @RequestParam(value = "serverType", required = false) Integer serverType,
    // @RequestParam(value = "totalCount", required = false) Integer totalCount,
    // @RequestParam(value = "packageId", required = false) Long packageId,
    // @RequestParam(value = "packageName", required = false) String
    // packageName) {
    // GridJsonData<PackageDefineDO> json = new GridJsonData<PackageDefineDO>();
    // PackageDefineQuery<PackageDefineDO> query = new
    // PackageDefineQuery<PackageDefineDO>();
    // query.setPageIndex(Integer.parseInt(pageIndex));
    // query.setPageSize(Integer.parseInt(pageSize));
    //
    // query.setPackageName(packageName);
    // query.setPackageId(packageId);
    // query.setServerType(serverType);
    // query.setTotalCount(totalCount);
    // packageDefineService.listByQuery(query);
    //
    // json.setQuery(query);
    // if (query.getDataList().isEmpty()) {
    // json.setErrorMessage("没有查询到符合条件的记录");
    // }
    // return json;
    // }
    //
    // @RequestMapping(value = "/add")
    // public String addMachine() {
    // return "packages/addpackagedefine";
    // }
    //
    // @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    // public @ResponseBody
    // BaseJson insert(@ModelAttribute("serviceid") final String serviceid,
    // @ModelAttribute("code") final String code,
    // @ModelAttribute("totalcount") final String totalcount,
    // @ModelAttribute("type") final Integer type) {
    // BaseJson retJson = new BaseJson();
    // if (StringUtils.isEmpty(code)) {
    // retJson.setErrorMessage("code为空");
    // retJson.setHasError(true);
    // return retJson;
    // }
    // if (null == type) {
    // retJson.setErrorMessage("type为空");
    // retJson.setHasError(true);
    // return retJson;
    // }
    // if (StringUtils.isEmpty(totalcount)) {
    // retJson.setErrorMessage("totalcount为空");
    // retJson.setHasError(true);
    // return retJson;
    // }
    // if (StringUtils.isEmpty(serviceid)) {
    // retJson.setErrorMessage("serviceid为空");
    // retJson.setHasError(true);
    // return retJson;
    // }
    // String[] serviceids = serviceid.split(";");
    // String shareKey = String.valueOf(System.currentTimeMillis());
    // for (int i = 0; i < serviceids.length; i++) {
    // PackageDefineDO define = new PackageDefineDO();
    // define.setServiceId(Long.valueOf(serviceids[i]));
    // define.setCode(code);
    // define.setTotalCount(Integer.valueOf(totalcount));
    // define.setServerType(type);
    // define.setShareKey(shareKey);
    // packageDefineService.add(define);
    // }
    //
    // retJson.setHasError(false);
    // retJson.setMessage("添加成功");
    // return retJson;
    // }
    //
    // @RequestMapping(value = "/delete")
    // public @ResponseBody
    // BaseJson deleteById(final Long id, HttpServletResponse response,
    // HttpServletRequest request) {
    // BaseJson retJson = new BaseJson();
    // if (id == null) {
    // retJson.setErrorMessage("未获取套餐对象");
    // retJson.setHasError(true);
    // return retJson;
    // }
    // packageDefineService.deleteById(id);
    // retJson.setHasError(false);
    // retJson.setMessage("删除成功");
    // return retJson;
    // }
}
